package com.wuzhuo.information.controller;

import com.wuzhuo.information.pojo.PageBean;
import com.wuzhuo.information.pojo.User;
import com.wuzhuo.information.utiles.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.ognl.ObjectElementsAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wuzhuo.information.service.userService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
public class userController {

    @Autowired
    private userService userService;

    @PostMapping("/register")
    public int register(@RequestBody User user) {
        log.info("注册新用户:{}",user);
        userService.save(user);
        return 1;
    }

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        log.info("用户:{}",user);
        User u = userService.login(user);

        //登录成功后下发JWT令牌
        if (u != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id",u.getId());
            claims.put("username",u.getUserName());

            String jwt = JwtUtils.generateJwt(claims);//Jwt令牌已经包含了当前登录的用户信息
            return jwt;
        }
        //登录失败，返回错误信息
        return "用户名或密码错误！";
    }

    @PostMapping("/read")
    public int read(@RequestBody List<Long> ids) {
        log.info("已读信息id:{}",ids);
        userService.read(ids);
        return 1;
    }
}
